<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
$show5N = getShow35($gametype);
$g_betname_3or5 = judge_type($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
require_once( "class_3d/Bet_3d.php" );
require_once( "class_3d/RateConfig_3d.php" );
require_once( "class_3d/SRate_3d.php" );
require_once( "class_3d/MyLotto_3d.php" );
include( "class_3d/class.lotto3d.php" );
$ui = new UI_3D(true);
$tpl = new Template2();
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("帐号已被暂停使用");
    exit();
}
$conf = new Config();
$RateConfig_3d = new RateConfig_3d($gametype);
$SRate_3d = new SRate_3d($gametype);
$MyLotto_3d = new MyLotto_3d($gametype);
$lotto3d = new lotto3d($gametype);
//2013-11-20
$Bet_3d = new Bet_3s($gametype);
$paperOpen = $RateConfig_3d->getPaperOpen();
$amountField = "balance";
$currAmount = $curruser['amount'];

$today = date("Y-m-d");
$bettypegroup = getvar("palygroup");
$bettype = getvar("bettype");
$num = getvar("num");
$showSubid = intval(getvar("subid"));
$showSubid = isset($RateConfig_3d->subArr[$showSubid]) ? $showSubid : "0";
$disk = intval(getvar("disk"));
if (!isset($curruser["disk{$disk}"]) || $curruser["disk{$disk}"] != 1)
{
    $ui->showErrorPage("盘类错误");
    exit();
}
if ($bettypegroup == "" || $bettype == "" || $num == "")
{
    $ui->showErrorPage("资料有误");
    exit();
}
else
{
    $bettype = intval($bettype);
}
unset($RateConfig_3d->playGroupArr['rcmp']);
unset($RateConfig_3d->playGroupArr['g6']);
unset($RateConfig_3d->playGroupArr['all']);
unset($RateConfig_3d->playGroupArr['r1pass']);
if (!isset($RateConfig_3d->playGroupArr[$bettypegroup]))
{
    $ui->showErrorPage("错误的玩法页面值");
    exit();
}
$RateConfig_3d->playGroupArr['g3'] = array("29");
if (!in_array($bettype, $RateConfig_3d->playGroupArr[$bettypegroup]))
{
    $ui->showErrorPage("错误的玩法值");
    exit();
}
if (!$RateConfig_3d->checkBetNumber($bettype, $num))
{
    $ui->showErrorPage("异常的下注内容！");
    exit();
}
switch ($show3D3S)
{
    case 0 :
        $lotto_data = $MyLotto_3d->getLotto3DByDate($today);
        $stopbet = $conf->get("stopbet_{$tbl}");
        $startbet = $conf->get("startbet_{$tbl}");
        break;
    case 1 :
        if ($lotto_data = $MyLotto_3d->getLotto3SByDateTime())
        {
            $stopbetInt = strtotime($lotto_data['stopbet']);
            $startbetInt = strtotime($lotto_data['startbet']);
            $stopbet = date("Hi", $stopbetInt);
            $startbet = date("Hi", $startbetInt);
        }
        break;
    default :
        $ui->showErrorPage("类别错误！");
        break;
}
if ($lotto_data == false)
{
    $ui->showErrorPage("目前没有{$tblName}可以下注期数！");
    exit();
}
$open = $lotto_data['open'];
$round = $lotto_data['round'];
$lotto_id = $lotto_data['id'];
$ratedata = "";
$getRatesNumArr = array();
$getRatesSNumArr = array();
if ($stopbet <= date("Hi"))
{
    $tempStr = "下注期数：" . d3($round) . "<br>开奖日：{$open}<br />已经截止下注！";
    $ui->showErrorPage($tempStr);
    exit();
}
else if ($show3D3S == 0 && date("Hi") <= $startbet)
{
    $ui->showErrorPage("{$tblName}尚未开盘或暂时停止下注，请稍後再试");
    exit();
}
else if ($show3D3S == 1 && time() <= $startbetInt)
{
    $ui->showErrorPage("{$tblName}尚未开盘或暂时停止下注，请稍後再试");
    exit();
}
else if (0 < count(array_diff_assoc(array(0 - 1, 0 - 1, 0 - 1), array($lotto_data['n1'], $lotto_data['n2'], $lotto_data['n3']))))
{
    $ui->showErrorPage("{$tblName}::第" . $lotto_data['round'] . "期<br />已截止下注！", true);
    $reVal = false;
    exit();
}
$usedamount = 0;
$betamount =$curruser['balance'];

$diskstr = $g_disk_code[$disk];
$rateArr = $SRate_3d->getRates($lotto_id, $disk, $bettype, $showSubid);
if ($rateArr == false)
{
    $ui->showErrorPage("{$tblName}系统维护中，请稍後再试");
    exit();
}
$numIdxTemp = array_search($num, $RateConfig_3d->getBetTypeNumber($bettype));
if ($numIdxTemp === false)
{
    write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "({$tbl})[betfast]异常的下注号码於betfast页面 -{$bettype}, {$num}");
    $ui->showErrorPage("{$tblName}系统维护中，请稍後再试 ");
    exit();
}
$numIdx = "r" . str_pad($num, 2, "0", STR_PAD_LEFT);
$u5id = $SRate_3d->getAcl5ID($curruser['id'], $curruser['acl']);
$rateSArr = $SRate_3d->getRatesS($lotto_id, $bettype, $u5id, $showSubid);
$getRatesNumArr = $SRate_3d->getRatesNumArr($lotto_id, $disk, $bettype, $showSubid);
$getRatesSNumArr = $SRate_3d->getRatesSNumArr($lotto_id, $u5id, $bettype, $showSubid);
if ($rateSArr)
{
    $rateArr[$numIdx] = $rateArr[$numIdx] - $rateSArr[$numIdx];
    $rate5 = $rateSArr[$numIdx];
}
else
{
    $rate5 = 0;
}

$ratedata = $RateConfig_3d->getRateFromRateArr($bettype, $num, $rateArr, $getRatesNumArr, $getRatesSNumArr);

$betmin_tmp = intval($conf->get("betmin_{$tbl}"));
$betmin = in_array($bettype, $g_2face_3d) ? $conf->get("betmin2face_{$tbl}") : $betmin_tmp;
if ($betmin <= 0)
{
    $betmin = $betmin_tmp;
}
$betmax = intval($conf->get("betmax"));
$bettypeSubid = $bettype . "_" . $showSubid;
if (!isset($g_betname_3or5[$bettypeSubid]))
{
    write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "({$tbl})异常的下注玩法於betfast页面 -{$bettypeSubid}, {$num}");
    $ui->showErrorPage("玩法错误, 无法下注！");
    exit();
}
$betname = "【{$g_betname_3or5[$bettypeSubid]}】";
if ($ratedata <= 1)
{
    $ui->showErrorPage("赔率数值错误, 无法下注！");
    exit();
}
$tpl->load("main", "bet_fast.htm");
$tpl->set(array("account" => $curruser['account'], "open" => $open, "round" => d3($round), "lotto_id" => $lotto_id, "account" => $curruser['account'], "amount" => max(0, $currAmount), "usedamount" => $usedamount, "betamount" => max(0, $betamount), "numstr" => $RateConfig_3d->getBetContent($bettype, $num . ""), "num" => $num, "rate" => trimfloat($ratedata, 1), "min" => $betmin, "max" => $betmax, "max_str" => $betmax, "betname" => $betname, "action" => "op.php?op=member_3d&fp=bet_fast&gametype={$gametype}", "palygroup" => $bettypegroup, "bettype" => $bettype, "subid" => $showSubid, "gametypeStr" => $tblName, "gametype" => $gametype, "diskStr" => $g_disk_name[$disk], "disk" => $disk));
$bettype2=$bettype;
if($bettype==74)
{
	if($num==10 || $num==11)
		$bettype2=77;
	else if($num==13 || $num==14)
		$bettype2=78;		
}
$rs1 = getdb("select * from amounts_{$tbl} where bettype={$bettype2} and subid = {$showSubid} and user_id=" . $curruser['id']);
$amount1 = $rs1->f['amount1'];
$amount2 = $rs1->f['amount2'];
$amount1 = $amount1 ? $amount1 : "0";
$amount2 = $amount2 ? $amount2 : "0";
$tpl->set("amount1", $amount1);
$tpl->set("amount2", $amount2);
$tpl->set("amount1_str", $amount1);
$tpl->set("amount2_str", $amount2);
$rs2 = getdb("!!select sum(amount) from bet_{$tbl} where numbers='{$num}' and bettype='{$bettype}' and subid={$showSubid} and user_id=" . $curruser['id'] . " and lotto_id={$lotto_id} and rame=0");
$numberamount = $rs2->f[0] ? $rs2->f[0] : 0;
$tpl->set("amountnum", "<tr><td>本号剩余额度：</td><td align=right><b>" . ( $amount2 - $numberamount ) . "</b></td></tr>");
$rs2->close();
$usemaxamount = $curruser['usemaxamount'];
$oneamount = $amount1;
$tpl->set("oneamount", "<tr><td>单注限额：</td> <td align=right><b>{$oneamount}</b></td>");
if (ispost())
{
    if (!isset($_SESSION['crtime']))
    {
        $_SESSION['crtime'] = systime();
    }
    else if ($_SESSION['crtime'] == systime())
    {
        exit();
    }
    $money = intval($_POST['money']);
    if ($money == 0)
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "使用外挂攻击下注金额为0");
        $ui->showErrorPage("不要随便攻击本站！你的 IP 已经被记录了！");
        exit();
    }
    else if ($money < $betmin)
    {
        $ui->showErrorPage("下注金额不能小於最低限额！");
        exit();
    }
    else if ($amount2 < $money + $numberamount)
    {
        $ui->showErrorPage("下注金额不能超过单项限额！");
        exit();
    }
    else if ($betamount < $money)
    {
        $ui->showErrorPage("下注金额不能超过可用额度！");
        exit();
    }
    else if ($usemaxamount && $curruser['maxamount'] < $money + $usedamount)
    {
        $ui->showErrorPage("下注金额不能超过本期限额！");
        exit();
    }
    else if ($oneamount < $money)
    {
        $ui->showErrorPage("下注金额不能超过单注限额！");
        exit();
    }
    else if ($ratedata <= 1)
    {
        $ui->showErrorPage("赔率数值错误！");
        exit();
    }
    $payoff = $money * ( $ratedata - 1 );
    $fields = array();
    $fields['user_id'] = $curruser['id'];
    $fields['lotto_id'] = $lotto_id;
    $fields['bettype'] = $bettype;
    $fields['subid'] = $showSubid;
    $fields['numbers'] = "'{$num}'";
    $fields['amount'] = $money;
    $fields['rate'] = $ratedata;
    $fields['rate5'] = $rate5;
    $fields['payoff'] = $payoff;
    $fields['disk'] = $disk;
    $fields['line_nums'] = "'" . $RateConfig_3d->getBetContent($bettype, $num) . "'";
    $fields['unitmoney'] = $money;
    $fields['State'] = "0";
    $fields['ip'] = "'" . getIP() . "'";
    $u2 = get_user($curruser['prev_id'], $gametype);
    $u3 = get_user($u2['prev_id'], $gametype);
    $u4 = get_user($u3['prev_id'], $gametype);
    $u5 = get_user($u4['prev_id'], $gametype);
    require_once( "class_3d/Amounts_3d.php" );
    $Amounts_3d = new Amounts_3d($gametype);
    $fields['back1'] = $Amounts_3d->getBackArr($bettype2, $curruser['id'], $disk, $showSubid);
    $fields['back2'] = $Amounts_3d->getBackArr($bettype2, $u2['id'], $disk, $showSubid);
    $fields['back3'] = $Amounts_3d->getBackArr($bettype2, $u3['id'], $disk, $showSubid);
    $fields['back4'] = $Amounts_3d->getBackArr($bettype2, $u4['id'], $disk, $showSubid);
    $fields['back5'] = $Amounts_3d->getBackArr($bettype2, $u5['id'], $disk, $showSubid);
    if ($u5['supe_account'] != "")
    {
        $rs = getdb("select id from k_user where acl=255 and account='" . $u5['supe_account'] . "'");
        $uidQ = $rs->f['id'];
        $rs->close();
        $fields['back6'] = $Amounts_3d->getBackArr($bettype2, $uidQ, $disk, $showSubid);
    }
    else
    {
        $fields['back6'] = "0";
    }
    $mpercent = $u2['mpercent'];
    $percent2 = $u2['percent0'];
    $percent3 = min($u3['percent1'], $u4['mpercent'] - $mpercent - $percent2);
    $percent4 = min($u4['percent1'], $u5['mpercent'] - $mpercent - $percent2 - $percent3);
    $upercent = 100 - $mpercent - $percent2 - $percent3 - $percent4;
    $fields['percent2'] = $mpercent;
    $fields['percent3'] = $percent2;
    $fields['percent4'] = $percent3;
    $fields['percent5'] = $percent4;
    $fields['percent255'] = $upercent;
    $check_percent = $fields['percent255'];
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    if (count($Amounts_3d->check_Amount_255($lotto_id, $bettype, $num, $money * $upercent / 100, $showSubid)))
    {
        $ui->showErrorPage("此注项总额度已满,无法再下注！");
        exit();
    }
    if ($check_percent < 0)
    {
        err("系统参数严重错误！");
    }
    $fields['r2'] = $money * $fields['percent3'] / 100;
    $fields['r3'] = $money * $fields['percent4'] / 100;
    $fields['r4'] = $money * $fields['percent5'] / 100;
    $fields['r6'] = $money * $fields['percent255'] / 100;
    $fields['createtime'] = systime();
    $fields['sessid'] = "'" . enBetSessid($fields) . "'";
    $_SESSION['crtime'] = $fields['createtime'];
    $rs2Q = getdb("!!select sum(amount) from bet_{$tbl} where numbers='{$num}' and bettype='{$bettype}' and subid={$showSubid} and user_id=" . $curruser['id'] . " and lotto_id={$lotto_id} and rame=0");
    $numberamount = $rs2Q->f[0] ? $rs2Q->f[0] : 0;
    if ($amount2 < $money + $numberamount)
    {
        $ui->showErrorPage("下注金额不能超过单项限额！");
        exit();
    }
    $insertFlag = $Bet_3d->dbInsert($fields);
    if ($insertFlag !== false)
    {
        $lotto3d->getBet_3D($insertFlag);
    }
    $ui->header();
    $tpl->load("succ", "succ.htm");
    if ($insertFlag !== false)
    {
        $tpl->set("betrs", " <tr><td colspan=\"2\" align=center class=\"green\">成功</td></tr> ");
    }
    else
    {
        $tpl->set("betrs", "<tr><td colspan=\"2\" align=center class=\"red\">失败</td></tr>");
    }
    $tpl->set("ordertype", "<font color=red>" . $betname . " " . $RateConfig_3d->getBetContent($bettype, $num) . "</font> @ <font color=red><b>" . floatval($ratedata) . "</b></font>");
    $tpl->set("money", $money);
    $tpl->set("payoff", $payoff);
    $tpl->show("succ");
    $ui->footer();
}
else
{
    if (ispost())
    {
        $money = intval($_POST['money']);
        $tpl->set("money", $money);
        $tpl->set("message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>");
    }
    else
    {
        $oldrate = floatval($_GET['rate']);
        if ($ratedata != $oldrate)
        {
            $tpl->set("message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>");
        }
    }
    $tpl->set("account", $curruser['account']);
    $ui->header();
    $tpl->show("main");
    $ui->footer();
}
?>